import formJs from "./regexDetail.js"; //新增弹框配置项
// import publicForm from "@/components/public/up-form.vue"; //弹框form---多选、照片
import publicForm from "@/components/public/el-form.vue"; //弹框form

import {
    saveOutsourcedtesting, //保存
    getOutsourcedtestingByFid, //详情
    
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid",
        "fCompanyname"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            positionData: [],
            positionData1: [],
            btnStatus: [],
            positionObj: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj,
            additionalObj: formJs.additionalObj,
            isEdit: false,
            currntData: null,
            dataInfo: null,
            startDates: null,
            treeData: [], //材料种类
            formData: {
                fId: "", //必传
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                recordfiles: [], //删除文件id集合
                fCommissiondates: this.common.day(0), //委托日期
                fCommissionname: "", //委托人
                fCommissionid: "", //委托人id
                fUnitname: "", //接收委托单位名称
                fUnitid: "", //接收委托单位id
                fOrdernumber: "", //委托单号
                fFileurl: "", //委托单附件
                fNote: "", //备注
                fChecktype: "",
            },
            formDatafileList: [], //工作计划资料
            btnShow: true,
            currentDetail: null,
            selectPostionObj: [],
            types: [],
            // 材料弹框
            materialRow: {},
            fCompanynames: null,
            fCompanyids: null,
            fFileurllist: [], //附件
        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    this.getDetail({
                        fId: this.currentDetail.fId
                    })
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanyids = newVal
                }
            },
            immediate: true,
        },
        fCompanyname: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal

                }
            },
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.btnShow = true
        if (this.statusNum == 1) { //编辑
            this.formData.fId = this.currentDetail.fId //按钮 
            this.getDetail({
                fId: this.currentDetail.fId
            })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = true;
            this.formObj[6].uploadSelect = true
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[6].uploadSelect = false
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            if (b == "fManufacturername") { //生产厂家
                this.formData.fManufacturername = data.label; //生产厂家
                this.formData.fManufacturerid = data.value //生产厂家id
            } else if (b == "fSuppliername") { //供应商名称
                this.formData.fSuppliername = data.label; //供应商
                this.formData.fSupplierid = data.value //供应商id
            } else if (b == 'fSpecification') {
                this.formData.fSpecification = data.value //供应商id
            }
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fFileurllist.length; index++) {
                if (file.uid == this.fFileurllist[index].uid) {
                    if (this.fFileurllist[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fFileurllist[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fFileurllist[index].atta.fId,
                        });
                    }
                    this.fFileurllist.splice(index, 1);
                }
            }
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getOutsourcedtestingByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.yjQmpClOutsourcedtesting;
                    this.formData = data;
                    this.formData.fFileurl = data.fFileurl;
                    this.fFileurllist = res.data.fFileurllist;
                    if (this.statusNum == 1) {
                        this.formData.recordfiles = [];
                        console.log(this.userInfo.userId);
                        // 委托人
                        if (data.fCommissionid == null || data.fCommissionid == "" || data.fCommissionname == null || data.fCommissionname == "") {
                            this.formData.fCommissionid = this.userInfo.userId;
                            this.formData.fCommissionname = this.userInfo.realName;
                        }
                        // 委托日期
                        if (data.fCommissiondates == null || data.fCommissiondates == "") {
                            this.formData.fCommissiondates = this.common.day(0)
                        }
                    }


                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }

            });
        },
        yj_saveForm() {
            this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(obj) {
            var data = {
                fId: obj.fId, //必传
                userId: obj.userId, //当前登录人id
                userName: obj.userName, //当前登录人姓名
                recordfiles: obj.recordfiles, //删除文件id集合
                fCommissiondates: obj.fCommissiondates, //委托日期
                fCommissionname: obj.fCommissionname, //委托人
                fCommissionid: obj.fCommissionid, //委托人id
                fUnitname: obj.fUnitname, //接收委托单位名称
                fUnitid: obj.fUnitid, //接收委托单位id
                fOrdernumber: obj.fOrdernumber, //委托单号
                fFileurl: obj.fFileurl, //委托单附件
                fNote: obj.fNote, //备注
                fCompanyid: obj.fCompanyid,
                fChecktype: obj.fChecktype,
            }
            this.loading = true;
            this.isLock = true
            saveOutsourcedtesting(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = true;
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = true;
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })

        },
    },
};